<?php
class SesionesController extends AppController {

	var $name = 'Sesiones';
	var $helpers =array('Access');
    var $uses = array('Cartilla','Ciclo','Sesione');
        
    function sesion_activa(){
    	$usuario_activo=$this->Access->getMy('username');
    	//Verificacion de sesion del sistema
    	if(!$usuario_activo){
    		$this->Session->setFlash(__('Por favor inicie sesion', true));
    		$this->redirect(array('controller'=>'users','action' => 'login'));
    	}
    }
    
	function index($cartilla = null,$ciclo=null) {
	    $this->sesion_activa();
		
		$this->Sesione->recursive = 0;
		$sesiones = $this->paginate('Sesione',array('Sesione.cartilla_id'=>$cartilla));
		$nombre_cartilla =$this->Cartilla->find('first',array('conditions'=>array('Cartilla.id'=>$cartilla)));
		$ciclos =$this->Ciclo->find('first',array('conditions'=>array('Ciclo.id'=>$ciclo)));		
	    $this->set(compact('sesiones', 'cartilla', 'nombre_cartilla','ciclo','ciclos'));
	}

	function view($id = null) {
		$this->sesion_activa();
	   
		if (!$id) {
			$this->Session->setFlash(__('Invalid sesione', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('sesione', $this->Sesione->read(null, $id));
	}

	function add($cartilla = null, $ciclo=null) {
	    $usuario_activo = $this->Access->getMy('username');
		
		if (!empty($this->data)) {
			$this->Sesione->create();
			if ($this->Sesione->save($this->data)) {
				$this->Session->setFlash(__('La sesi&oacute;n ha sido guardada', true));
				$this->redirect(array('action' => 'index',$this->data['Sesione']['cartilla']."/".$this->data['Sesione']['ciclo_id']));
			} else {
				$this->Session->setFlash(__('La sesi&oacute;n no ha sido guardada. Favor, intente mas tarde.', true));
			}
		}
		$cartillas = $this->Sesione->Cartilla->find('list',array('fields'=>array('Cartilla.id','Cartilla.nombre')));
		//Informacion Cartilla Activa
		$info_cartilla_activa = $this->Sesione->Cartilla->find('first',array('conditions'=>array('Cartilla.id'=>$cartilla)));
			
		$participantes = $this->Sesione->Participante->find('list');
		$ciclos = $this->Cartilla->Ciclo->read(null,$ciclo);	
		$this->set(compact('cartillas', 'participantes','info_cartilla_activa','cartilla','ciclos'));
	}

	function edit($id = null,$cartilla = null, $ciclo=null) {
	    $this->sesion_activa();
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid sesione', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Sesione->save($this->data)) {
				$this->Session->setFlash(__('La sesi&oacute;n ha sido actualizada', true));
				$this->redirect(array('action' => 'index',$this->data['Sesione']['cartilla']."/".$this->data['Sesione']['ciclo_id']));
			} else {
				$this->Session->setFlash(__('La sesi&oacute;n no ha sido actualizada. Favor, intente mas tarde.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Sesione->read(null, $id);
		}
		$cartillas = $this->Sesione->Cartilla->find('list',array('fields'=>array('Cartilla.id','Cartilla.nombre')));
		//Informacion Cartilla Activa
		$info_cartilla_activa = $this->Sesione->Cartilla->find('first',array('conditions'=>array('Cartilla.id'=>$cartilla)));
			
		$participantes = $this->Sesione->Participante->find('list');
		$ciclos = $this->Cartilla->Ciclo->read(null,$ciclo);	
		$this->set(compact('cartillas', 'participantes','info_cartilla_activa','cartilla','ciclos'));
	}

	function delete($id = null) {
	   $this->sesion_activa();
	   
		if (!$id) {
			$this->Session->setFlash(__('Sesi&oacute;n invalida', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Sesione->delete($id)) {
			$this->Session->setFlash(__('Sesi&oacute;n eliminada', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Sesione was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function get_sesiones_json($cartilla = null){
		if( $cartilla != null ){
			$sesiones = $this->Sesione->find('list', array("fields"=>array("Sesione.id", "Sesione.nombre"),
					"conditions"=>array("Sesione.cartilla_id"=>$cartilla)));
				
			$this->view = 'Json';
			$this->set(compact('sesiones'));
			$this->set('json', 'sesiones');
				
		}
	}
   
}
